package com.yuanlrc.base.controller.admin;

import com.yuanlrc.base.bean.CodeMsg;
import com.yuanlrc.base.bean.PageBean;
import com.yuanlrc.base.bean.Result;
import com.yuanlrc.base.dao.admin.StudentDao;
import com.yuanlrc.base.entity.admin.Book;
import com.yuanlrc.base.entity.admin.Borrow;
import com.yuanlrc.base.entity.admin.Student;
import com.yuanlrc.base.service.admin.BookService;
import com.yuanlrc.base.service.admin.BorrowService;
import com.yuanlrc.base.service.admin.OperaterLogService;
import com.yuanlrc.base.service.admin.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 借阅管理控制器
 */
@Controller
@RequestMapping("/borrow")
public class BorrowController {

    @Autowired
    private BookService  bookService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private BorrowService borrowService;
    @Autowired
    private OperaterLogService operaterLogService;

    /**
     * 分页搜索借阅列表
     * @param model
     * @param borrow
     * @param pageBean
     * @return
     */
    @RequestMapping(value = "/list")
    public String borrowList(Model model, Borrow borrow, PageBean<Borrow>pageBean){
        model.addAttribute("title","借阅列表");
        model.addAttribute("pageBean", borrowService.findByName(borrow, pageBean));
        return "/admin/borrow/list";
    }

    /**
     * 归还图书功能
     * @param id
     * @return
     */
    @RequestMapping(value = "/returnBook",method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean>returnBook(@RequestParam(name = "id",required = true)Long id){
            boolean flag = borrowService.returnBook(id);
            if (flag){
                operaterLogService.add("归还书籍,归还ID："+id);
            }else {
                return Result.error(CodeMsg.ADMIN_BOOK_RETURN_ERROR);
            }
        return Result.success(true);
    }

}
